﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenerPro;

namespace MensUnoShop.Models
{
    [Serializable]
    public class Member
    {
        protected Guid _mguid;
        protected DateTime _signup;
        protected string _firstname;
        protected string _lastname;
        protected string _password;
        protected bool _sex;
        protected bool _ismarried;
        protected string _birth;
        protected string _email;
        protected string _telh;
        protected string _telo;
        protected string _mobile;
        protected string _zip;
        protected string _city;
        protected string _area;
        protected string _address;
        protected string _edu;
        protected string _job;
        protected string _income;
        protected string _country;
        protected bool _isedm;
        protected bool _status;
        protected DateTime _lastlogon;
        protected List<Coupon> _coupon;
        [NonSerialized]
        protected DBMember _repository;
        [NonSerialized]
        protected SenerPro.Logger _log;

        public Guid MGUID { set { _mguid = value; } get { return _mguid; } }
        public DateTime SIGNUP { set { _signup = value; } get { return _signup; } }
        public string FIRSTNAME { set { _firstname = value; } get { return _firstname; } }
        public string LASTNAME { set { _lastname = value; } get { return _lastname; } }
        public string PASSWORD { set { _password = value; } get { return _password; } }
        public bool SEX { set { _sex = value; } get { return _sex; } }
        public bool ISMARRIED { set { _ismarried = value; } get { return _ismarried; } }
        public string BIRTH { set { _birth = value; } get { return _birth; } }
        public string EMAIL { set { _email = value; } get { return _email; } }
        public string TELO { set { _telo = value; } get { return _telo; } }
        public string TELH { set { _telh = value; } get { return _telh; } }
        public string MOBILE { set { _mobile = value; } get { return _mobile; } }
        public string ZIP { set { _zip = value; } get { return _zip; } }
        public string CITY { set { _city = value; } get { return _city; } }
        public string AREA { set { _area = value; } get { return _area; } }
        public string ADDRESS { set { _address = value; } get { return _address; } }
        public string COUNTRY { set { _country = value; } get { return _country; } }
        public string JOB { set { _job = value; } get { return _job; } }
        public string INCOME { set { _income = value; } get { return _income; } }
        public string EDU { set { _edu = value; } get { return _edu; } }
        public DateTime LASTLOGON { set { _lastlogon = value; } get { return _lastlogon; } }
        public List<Coupon> COUPON { set { _coupon = value; } get { return _coupon; } }
        public bool ISEDM { set { _isedm = value; } get { return _isedm; } }
        public bool STATUS { set { _status = value; } get { return _status; } }
        public Member() { _mguid = Guid.Empty; }
        public Member(string email) { _email = email; }

        public void SetRepository()
        {
            if (_repository == null)
                _repository = new DBMember();
        }

        public void GetMemberData()
        {
            SetRepository();
            var m = _repository.GetMemberData(_email);
            if (m != null)
            {
                this.MGUID = m.MGUID;
                this.FIRSTNAME = m.FIRSTNAME;
                this.LASTNAME = m.LASTNAME;
                this.EMAIL = m.EMAIL;
                this.ISMARRIED = m.ISMARRIED;
                this.SIGNUP = m.SIGNUP;
                this.BIRTH = m.BIRTH;
                this.SEX = m.SEX;
                this.MOBILE = m.MOBILE;
                this.TELO = m.TELO;
                this.TELH = m.TELH;
                this.COUNTRY = m.COUNTRY;
                this.ZIP = m.ZIP;
                this.CITY = m.CITY;
                this.AREA = m.AREA;
                this.EDU = m.EDU;
                this.JOB = m.JOB;
                this.INCOME = m.INCOME;
                this.ADDRESS = m.ADDRESS;
                this.LASTLOGON = m.LASTLOGON;
            }
        }

        public void UptMemberLastLogin(string ip)
        {
            SetRepository();
            _repository.UptMemberLastLogin(_mguid, ip);
            this.LASTLOGON = DateTime.Now;
        }

        public void UptCookie()
        {
            HttpContext.Current.Session["mm"] = this;
            HttpCookie hk = new HttpCookie("mm", BlowFish.Encrypt(Tools.ToBase64URLString(Tools.BinarySerialize(this)), "mavi"));
            HttpContext.Current.Response.Cookies.Add(hk);
        }

        public void GetCoupon()
        {
            SetRepository();
            _coupon = _repository.GetMemberCouponList(_mguid);
        }

        public bool SetCoupon(string ticket)
        {
            SetRepository();
            if (_repository.SetCoupon(_mguid, ticket))
            {
                _coupon = _repository.GetMemberCouponList(_mguid);
                return true;
            }
            return false;
        }

        public bool SetCoupon(string ticket, ref int mcid)
        {
            SetRepository();
            if (_repository.SetCoupon(_mguid, ticket, ref mcid))
            {
                _coupon = _repository.GetMemberCouponList(_mguid);
                return true;
            }
            return false;
        }

        public void ResetCoupon(int mcid)
        {
            SetRepository();
            _repository.ResetCoupon(mcid);
        }
    }
}